#!/usr/bin/python

# imgr

import sys,os,re,optparse
import matplotlib
if "DISPLAY" not in os.environ: matplotlib.use("AGG")
else: matplotlib.use("GTK")
import signal
signal.signal(signal.SIGINT,lambda *args:sys.exit(1))
from pylab import *
from scipy.ndimage import measurements
from scipy.misc import imsave
from PIL import Image

parser = optparse.OptionParser(usage="""
%prog [options] page1.png page2.png ...

Computes text/image segmentation of grayscale input images.
""")
parser.add_option("-d","--display",help="display result",action="store_true")
parser.add_option("-D","--Display",help="display continuously",action="store_true")
parser.add_option("-r","--dpi",help="resolution in DPI",type=float,default=200.0)
parser.add_option("-c","--close",help="closing",type=int,default=0)
parser.add_option("-o","--open",help="opening",type=int,default=2)
parser.add_option("-S","--segmentation",help="segmentation component",default="ocrolib.SimpleTIClassification")
options,args = parser.parse_args()

if options.Display: options.display = 1
if options.display: ion(); show()

tisegmenter = ocrolib.make_ITextImageClassification(options.segmentation)

count = 1

for arg in args:
    print
    print "===",arg,"==="
    print

    image = ocrolib.read_image_gray(arg)

    result = tisegmenter.textImageProbabilities(image,options.open,options.close)

    if options.display: 
        clf(); imshow(NI(result),cmap=cm.gray); draw()
        if not options.Display: 
            raw_input("hit ENTER to continue")
        else:
            ginput(1,timeout=1)

    base,_ = ocrolib.allsplitext(arg)
    ocrolib.write_image_packed(base+".tiseg.png",result)
    count += 1
